package com.zk.service.impl;

import com.zk.dao.*;
import com.zk.po.Comment;
import com.zk.po.DyPicture;
import com.zk.po.Dynamic;
import com.zk.po.Follow;
import com.zk.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kun
 * @since 2022-06-08
 */
@Service
public class DynamicServiceImpl extends ServiceImpl<DynamicMapper, Dynamic> implements DynamicService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private DyPictureMapper dyPictureMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private DynamicMapper dynamicMapper;
    @Resource
    private FollowMapper followMapper;
    @Resource
    private LikeMapper likeMapper;


    //完善动态信息
    @Override
    public List<Dynamic> getDynamicData(List<Dynamic> dynamicList) {
        int i = 1;
        //这些动态的评论信息得遍历动态插入
        for (Dynamic dy : dynamicList) {
            try {
                System.out.println("设置第" + i++ + "条");
                List<Comment> dynamicComment = commentMapper.findCommentByDynamicId(dy.getId());
                for (Comment co : dynamicComment) {
                    try {
                        //嵌套循环,把评论的评论也放进去
                        co.setCommentList(commentMapper.findCommentsByCommentId(co.getId()));
                        co.setUser(userMapper.findUserById(co.getUserId()));
                        co.setCommentVisible(false);
                        co.setLikeVisible(false);
                        for (Comment coo : co.getCommentList()) {
                            //理论上这里不会出问题
                            coo.setUser(userMapper.findUserById(coo.getUserId()));
                            coo.setRespondWho(userMapper.findUserById(commentMapper.findCommentByCommentId(coo.getCommentId()).getUserId()).getNickName());

                        }

                    } catch (Exception e) {
                        System.out.println("该评论的评论为空");
                    }
                }
                dy.setCommentList(dynamicComment);
                dy.setUser(userMapper.findUserById(dy.getUserId()));
                dy.setCommentVisible(false);
                dy.setLikeVisible(false);
                try {
                    List<DyPicture> imageList = dyPictureMapper.findDynamicImageListById(dy.getId());
                    dy.setImageList(imageList);
                } catch (Exception e) {
                    dy.setImageList(Collections.emptyList());
                    System.out.println("该动态的图片为空");
                }
            } catch (Exception e) {
                System.out.println("该动态评论为空");
            }
        }
        return dynamicList;
    }

    //获取主页的动态
    @Override
    public List<Dynamic> getMainDyamic() {
        //先给数据库更新一下数据,总共运行两遍
        List<Dynamic> dynamicCard = dynamicMapper.findDynamicListByTime();
        //i是一个变量记录一下第几条好调试
        int i = 1;
        for (Dynamic dy : dynamicCard) {
            try {
                System.out.println("设置第" + i++ + "条动态的点赞数和评论数");
                dynamicMapper.updateaDynamicNumDataById(dy.getId());
                List<Comment> dynamicComment = commentMapper.findCommentByDynamicId(dy.getId());
                for (Comment co : dynamicComment) {
                    try {
                        commentMapper.updateaCommentNumDataById(co.getId());
                    } catch (Exception e) {
                        System.out.println("该评论的评论为空");
                    }
                }
            } catch (Exception e) {
                System.out.println("该动态评论为空");
            }
        }
        //动态的信息
        dynamicCard = dynamicMapper.findDynamicListByTime();
        return getDynamicData(dynamicCard);
    }

    //获取登录用户主动态
    @Override
    public List<Dynamic> getUserDynamic(List<Dynamic> dynamicList,String id) {
        for (Dynamic dy : dynamicList) {
            dy.setLikeVisible(likeMapper.ifDynamicLikeByUser(dy.getId(),id)!=null);
        }
        return dynamicMapper.findDynamicListByUserId(id);
    }

    //获取登录用户关注人动态
    @Override
    public List<Dynamic> getDynamicByFollow(String id) {
        List<Follow> followList = followMapper.findLeaderByFollow(id);
        System.out.println(followList);
        List<Dynamic> dynamicList_leaders = new ArrayList<>();
        for (Follow fo : followList) {
            List<Dynamic> dynamicList = dynamicMapper.findDynamicListByUserId(fo.getLeader());
            dynamicList_leaders.addAll(dynamicList);
        }
        //动态的信息
        return getDynamicData(dynamicList_leaders);
    }


}
